﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AvantGameMachine.Graphics
{
    public class RenderList
    {
        private List<IRenderizableObject> renderList = new List<IRenderizableObject>();

        public RenderList()
        {
        }

        public void Add(IRenderizableObject _newObject)
        {
            bool Inserted = false;

            for (int i = 0; i < renderList.Count; i++)
            {
                if (renderList[i].ROrder > _newObject.ROrder)
                {
                    renderList.Insert(i, _newObject);
                    Inserted = true;
                    i = renderList.Count;
                }
            }

            if (!Inserted)
                renderList.Add(_newObject);
        }

        public void Reorder(IRenderizableObject _obj)
        {
            renderList.Remove(_obj);
            Add(_obj);
        }

        internal void InternalRender()
        {
            try
            {
                IRenderizableObject[] rolist = renderList.ToArray();
                foreach (IRenderizableObject obj in rolist)
                {
                    obj.InternalRender();
                }
            }
            catch (Exception e)
            {
                AGMPlatform.log.Debug(e.ToString());
            }
        }

        internal int Count
        {
            get { return renderList.Count(); }
        }

        internal IRenderizableObject GetByIndex(int _index)
        {
            return renderList[_index];
        }

        internal bool Remove(IRenderizableObject _obj)
        {
            return renderList.Remove(_obj);
        }

        internal IRenderizableObject[] ToArray()
        {
            return renderList.ToArray();
        }

        internal void Clear()
        {
            renderList.Clear();
        }

        internal bool AreChildrenTransformed()
        {
            foreach (IRenderizableObject obj in renderList)
            {
                if (obj.children.AreChildrenTransformed())
                    return true;
            }

            return false;
        }
    }
}
